package com.powernode.controller;

import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.Customer;
import com.powernode.domain.User;
import com.powernode.dto.CustomerDto;
import com.powernode.service.CustomerService;
import com.powernode.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 *客户操作
 */
@RestController
@RequestMapping("customer")
public class    CustomerController {

    @Resource
    private UserService userService;

    @Resource
    private CustomerService customerService;

    @GetMapping("query.action")
    public Result query(CustomerDto customerDto, HttpSession session){
        //判断当前用户是否为管理员
        User user= (User) session.getAttribute(Constants.SESSION_USER_KEY);
        if(user.getRole().equals(Constants.USER_TYPE_NORMAL)){
            customerDto.setUserId(user.getId());
        }

        Page<Customer> page = this.customerService.queryForPage(customerDto);
        return new Result(0,"查询成功",page.getTotal(),page.getResult());
    }

    //添加
    @PostMapping("add.action")
    public Result add(Customer customer, HttpSession session){

        //设置在职状态
        customer.setDeleted(Constants.USER_DELETED_1);


        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);

        //设置默认头像
        customer.setUserId(user.getId());

        //设置创建时间
        customer.setCreateTime(new Date());

        //保存
        try {
            this.customerService.insertSelective(customer);
            return new Result(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }


    //更新
    @PostMapping("update.action")
    public Result update(Customer customer){

        try {
            this.customerService.updateByPrimaryKeySelective(customer);
            return new Result(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"修改失败");
        }
    }


    //删除
    @PostMapping("del.action")
    public Result del(Integer id){
        Customer customer = new Customer();
        customer.setId(id);
        customer.setDeleted(Constants.USER_DELETED_2);
        customer.setDeleteTime(new Date());
        try {
            this.customerService.updateByPrimaryKeySelective(customer);
            return new Result(200,"离职成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"离职失败");
        }
    }

    //修改客户所属的业务员
    @PostMapping("changeCustomerUser.action")
    public Result changeCustomerUser(Integer userId,Integer[] customerIds){
        if (userId==null){
            return new Result(-1,"请选择业务员ID");
        }
        if (customerIds==null || customerIds.length==0){
            return new Result(-1,"请选择需要转移的客户");
        }
        try {
            this.customerService.changeCustomerUser(userId,customerIds);
            return new Result(200,"转移成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1,"转移失败");
        }
    }
}
